package com.xinghou.platform.file.log;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public abstract class AbstractAopLog {
	
	public abstract Logger getErrorLog();
	
	public abstract Logger getMonitorLog();

	public Object logExecuteTime(ProceedingJoinPoint pjp) throws Throwable{
		long start = System.currentTimeMillis();
		try{
			return pjp.proceed();
		}finally{
			long time = System.currentTimeMillis()-start;
			String methodName = pjp.getSignature().getName();
			StringBuffer sbInfo = new StringBuffer();
			sbInfo.append("call method ");
			sbInfo.append(methodName);
			sbInfo.append("(");
			Object[] args = pjp.getArgs();
			int len = args.length;
			for(int i=0;i<len;i++){
				sbInfo.append(args[i]==null?"null" : args[i].toString());
				if(i < (len -1)){
					sbInfo.append(", ");
				}
			}
			sbInfo.append(") execute time: ");
			sbInfo.append(time);
			getMonitorLog().info(sbInfo.toString());
		}
	}

	public void logException(JoinPoint jp, Throwable ex){
		String methodName = jp.getSignature().getName();
		StringBuffer sbInfo = new StringBuffer();
		sbInfo.append("call method ");
		sbInfo.append(methodName);
		sbInfo.append("(");
		Object[] args = jp.getArgs();
		int len = args.length;
		for(int i=0;i<len;i++){
			sbInfo.append(args[i]==null?"null" : args[i].toString());
			if(i < (len -1)){
				sbInfo.append(", ");
			}
		}
		sbInfo.append(") error:\n");
		sbInfo.append(ex.toString());
		getErrorLog().error(sbInfo.toString());
	}
}
